<html>
<head><meta charset="utf-8"><title>fmt::Result doesn&#x27;t allow having an error message · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/fmt.3A.3AResult.20doesn&#x27;t.20allow.20having.20an.20error.20message.html">fmt::Result doesn&#x27;t allow having an error message</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="212976934"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/fmt%3A%3AResult%20doesn%27t%20allow%20having%20an%20error%20message/near/212976934" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/fmt.3A.3AResult.20doesn&#x27;t.20allow.20having.20an.20error.20message.html#212976934">(Oct 11 2020 at 18:51)</a>:</h4>
<p>From Marcus Klaas on <a href="https://github.com/raphlinus/pulldown-cmark/pull/493">https://github.com/raphlinus/pulldown-cmark/pull/493</a>:</p>
<blockquote>
<p>The implementation looks good, but I just now noticed that <code>fmt::Write</code> has its own error. This means that it's no longer possible to pass along IO errors. If a write fails to a <code>Write</code> object, the user will have no way to discover why. This seems like a serious drawback of the change.</p>
</blockquote>
<p>It does seem unfortunate that AFAICT you can't return error messages from an impl of <code>fmt::Write</code>, yet you can from <code>io::Write</code>. Is there a reason for this, and does anyone have ideas for what to do?</p>



<a name="212977150"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/fmt%3A%3AResult%20doesn%27t%20allow%20having%20an%20error%20message/near/212977150" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/fmt.3A.3AResult.20doesn&#x27;t.20allow.20having.20an.20error.20message.html#212977150">(Oct 11 2020 at 18:57)</a>:</h4>
<p>fmt::Write is in core rather than std</p>



<a name="212979046"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/fmt%3A%3AResult%20doesn%27t%20allow%20having%20an%20error%20message/near/212979046" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/fmt.3A.3AResult.20doesn&#x27;t.20allow.20having.20an.20error.20message.html#212979046">(Oct 11 2020 at 19:52)</a>:</h4>
<p>Hmm, why is that relevant?</p>



<a name="212980880"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/fmt%3A%3AResult%20doesn%27t%20allow%20having%20an%20error%20message/near/212980880" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/fmt.3A.3AResult.20doesn&#x27;t.20allow.20having.20an.20error.20message.html#212980880">(Oct 11 2020 at 20:43)</a>:</h4>
<p>io::Error allocates so it can't be in core</p>



<a name="212981404"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/fmt%3A%3AResult%20doesn%27t%20allow%20having%20an%20error%20message/near/212981404" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/fmt.3A.3AResult.20doesn&#x27;t.20allow.20having.20an.20error.20message.html#212981404">(Oct 11 2020 at 20:59)</a>:</h4>
<p>(Also, it's often annoying that io::Error allocates and contains a box.)</p>



<a name="212981638"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/fmt%3A%3AResult%20doesn%27t%20allow%20having%20an%20error%20message/near/212981638" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/fmt.3A.3AResult.20doesn&#x27;t.20allow.20having.20an.20error.20message.html#212981638">(Oct 11 2020 at 21:05)</a>:</h4>
<p>Is there any way to solve this then?</p>



<a name="212981675"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/fmt%3A%3AResult%20doesn%27t%20allow%20having%20an%20error%20message/near/212981675" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/fmt.3A.3AResult.20doesn&#x27;t.20allow.20having.20an.20error.20message.html#212981675">(Oct 11 2020 at 21:06)</a>:</h4>
<p>As a side note as far as I know while <code>fmt</code> based formatting can fail, it <em>rally</em> should never do so as far as possible. (As already mentioned <code>fmt::Error</code> lives in core, which means it can not depend on thinks like <code>String</code> or <code>Box</code>).</p>



<a name="212981772"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/fmt%3A%3AResult%20doesn%27t%20allow%20having%20an%20error%20message/near/212981772" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/fmt.3A.3AResult.20doesn&#x27;t.20allow.20having.20an.20error.20message.html#212981772">(Oct 11 2020 at 21:09)</a>:</h4>
<p>In a certain way failing <code>Display</code>/<code>Debug</code> formatting is like stating "this type is internally in such a inconsistent state that we can't format it anymore".  Which shouldn't be possible in the first case. Through there are use-cases where failing can make sense, but in many of this not using <code>fmt</code> formating also makes sense. (EDIT: To be clear I have run into this myself one or two times but every time it turned out to be a good idea to change the code to not use <code>fmt</code>-formatting for what I did use it before.)</p>



<a name="212981871"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/fmt%3A%3AResult%20doesn%27t%20allow%20having%20an%20error%20message/near/212981871" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/fmt.3A.3AResult.20doesn&#x27;t.20allow.20having.20an.20error.20message.html#212981871">(Oct 11 2020 at 21:11)</a>:</h4>
<p>Maybe <code>fmt::Error</code> could  be extended to contain a <code>Option&lt;&amp;'static str&gt;</code>. Which should be technical possible. But there might be some reasons not to do so. But I'm not aware of any. Opening a pre-RFC thread (or similar) on the internals forum could make sense, I guess.</p>



<a name="212983152"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/fmt%3A%3AResult%20doesn%27t%20allow%20having%20an%20error%20message/near/212983152" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/fmt.3A.3AResult.20doesn&#x27;t.20allow.20having.20an.20error.20message.html#212983152">(Oct 11 2020 at 21:49)</a>:</h4>
<p>I just noticed the error type is declared as <code>pub struct Error;</code>. (I thought it was something like <code>pub struct Error { _priv: () }</code>;). This means that adding any fields is a braking change. So I guess we are stuck with it?</p>



<a name="212986433"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/fmt%3A%3AResult%20doesn%27t%20allow%20having%20an%20error%20message/near/212986433" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/fmt.3A.3AResult.20doesn&#x27;t.20allow.20having.20an.20error.20message.html#212986433">(Oct 11 2020 at 23:20)</a>:</h4>
<p>Yes, I was just going to mention that :/</p>



<a name="212990062"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/fmt%3A%3AResult%20doesn%27t%20allow%20having%20an%20error%20message/near/212990062" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/fmt.3A.3AResult.20doesn&#x27;t.20allow.20having.20an.20error.20message.html#212990062">(Oct 12 2020 at 01:27)</a>:</h4>
<p>Separate from being a breaking change, making fmt::Error not be a ZST could hurt performance,</p>
<p>core::fmt::Write::write_{str,char,fmt} gives a <code>Result&lt;(), core::fmt::Error&gt;</code>, which works out to just being a bool. If you make the return type <code>Result&lt;(), Option&lt;&amp;'static str&gt;&gt;</code>that's suddenly a <code>3*usize</code> big value, which IIRC doesn't cleanly pass in just registers when returning.</p>



<a name="213006063"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/fmt%3A%3AResult%20doesn%27t%20allow%20having%20an%20error%20message/near/213006063" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/fmt.3A.3AResult.20doesn&#x27;t.20allow.20having.20an.20error.20message.html#213006063">(Oct 12 2020 at 07:37)</a>:</h4>
<p>I think ideally the trait would have an associated type for the Error?</p>



<a name="213006774"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/fmt%3A%3AResult%20doesn%27t%20allow%20having%20an%20error%20message/near/213006774" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/fmt.3A.3AResult.20doesn&#x27;t.20allow.20having.20an.20error.20message.html#213006774">(Oct 12 2020 at 07:46)</a>:</h4>
<p>i feel that would not be very ideal, that would spread out an associated type across way too much of the ecosystem</p>



<a name="213052535"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/fmt%3A%3AResult%20doesn%27t%20allow%20having%20an%20error%20message/near/213052535" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/fmt.3A.3AResult.20doesn&#x27;t.20allow.20having.20an.20error.20message.html#213052535">(Oct 12 2020 at 15:25)</a>:</h4>
<p>How is that different from other traits with associated types? It can also just be generic over the error without an acossiated type, but that will probably be worse</p>



<a name="213066915"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/fmt%3A%3AResult%20doesn%27t%20allow%20having%20an%20error%20message/near/213066915" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/fmt.3A.3AResult.20doesn&#x27;t.20allow.20having.20an.20error.20message.html#213066915">(Oct 12 2020 at 17:41)</a>:</h4>
<p>I think the trouble with an associated type there would be that you couldn't mix errors from different implementations any more, which feels pretty likely something you'd want.  Like writing to an array for a bit, then from there to a file, or whatever...</p>
<p>Things like the error in <code>TryFrom</code> don't have quite that same mixing awkwardness.</p>



<a name="213067202"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/fmt%3A%3AResult%20doesn%27t%20allow%20having%20an%20error%20message/near/213067202" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/fmt.3A.3AResult.20doesn&#x27;t.20allow.20having.20an.20error.20message.html#213067202">(Oct 12 2020 at 17:44)</a>:</h4>
<p>Hmm, the fact that <code>.to_string()</code> doesn't even bother returning a <code>Result</code> is an indication to me that these things are <em>really</em> not supposed to ever fail...</p>



<a name="213071652"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/fmt%3A%3AResult%20doesn%27t%20allow%20having%20an%20error%20message/near/213071652" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/fmt.3A.3AResult.20doesn&#x27;t.20allow.20having.20an.20error.20message.html#213071652">(Oct 12 2020 at 18:31)</a>:</h4>
<blockquote>
<p>However, they should never return errors spuriously. That is, a formatting implementation must and may only return an error if the passed-in Formatter returns an error. This is because, contrary to what the function signature might suggest, string formatting is an infallible operation. This function only returns a result because writing to the underlying stream might fail and it must provide a way to propagate the fact that an error has occurred back up the stack.</p>
</blockquote>



<a name="213072808"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/fmt%3A%3AResult%20doesn%27t%20allow%20having%20an%20error%20message/near/213072808" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/fmt.3A.3AResult.20doesn&#x27;t.20allow.20having.20an.20error.20message.html#213072808">(Oct 12 2020 at 18:41)</a>:</h4>
<p>Though the reason I would want it to return an error is if writing to e.g. a file fails, and I think that should be covered by:</p>
<blockquote>
<p>This function only returns a result because writing to the underlying stream might fail</p>
</blockquote>



<a name="213073116"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/fmt%3A%3AResult%20doesn%27t%20allow%20having%20an%20error%20message/near/213073116" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/fmt.3A.3AResult.20doesn&#x27;t.20allow.20having.20an.20error.20message.html#213073116">(Oct 12 2020 at 18:44)</a>:</h4>
<p>It is perhaps worth noting that io::Write::write_fmt <em>does</em> preserve the io Error in the raw form (<a href="https://doc.rust-lang.org/nightly/src/std/io/mod.rs.html#1498-1513">https://doc.rust-lang.org/nightly/src/std/io/mod.rs.html#1498-1513</a>)</p>



<a name="213073138"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/fmt%3A%3AResult%20doesn%27t%20allow%20having%20an%20error%20message/near/213073138" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/fmt.3A.3AResult.20doesn&#x27;t.20allow.20having.20an.20error.20message.html#213073138">(Oct 12 2020 at 18:44)</a>:</h4>
<p>so if you're using write! or similar with io sources, and not the fmt trait directly, you'll get the error</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>